package org.szgb.console.sys.system.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysSystem extends BeanEntity<SysSystem> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;

    public SysSystem() {
    }

    public SysSystem(String id) {
        this.setId(id);
    }

    public SysSystem(SysSystemFindDto sysSystemFindDto){
        this.setId(sysSystemFindDto.getId());
        this.setName(sysSystemFindDto.getName());
        this.setPageDto(sysSystemFindDto.getPageDto());
    }

    public SysSystem(SysSystemSaveDto sysSystemSaveDto){
        this.setId(sysSystemSaveDto.id);
        this.setName(sysSystemSaveDto.name);
    }

    public SysSystemView toSysSystemView() {
        return new SysSystemView(this);
    }

    @Data
    @Schema(title = "系统信息", description = "系统信息")
    public static class SysSystemView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysSystemView(SysSystem sysSystem) {
            this.id = sysSystem.getId();
            this.name = sysSystem.getName();
            this.createdAt = sysSystem.getCreatedAt();
            this.updatedAt = sysSystem.getUpdatedAt();
            if (sysSystem.getCreatedBy() != null) {
                this.createdById = sysSystem.getCreatedBy().getId();
                this.createdByName = sysSystem.getCreatedBy().getName();
            }
            if (sysSystem.getUpdatedBy() != null) {
                this.updatedById = sysSystem.getUpdatedBy().getId();
                this.updatedByName = sysSystem.getUpdatedBy().getName();
            }
        }

        public static List<SysSystemView> getViewListFromList(List<SysSystem> sysSystemList) {
            return sysSystemList.stream().map(SysSystemView::new).toList();
        }
    }

    @Data
    @Schema(title = "系统查询表单", description = "系统查询参数")
    public static class SysSystemFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysSystem toSysSystem() {
            return new SysSystem(this);
        }
    }

    @Data
    @Schema(title = "系统保存表单", description = "系统保存参数")
    public static class SysSystemSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "名称")
        private String name;

        public SysSystem toSysSystem() {
            return new SysSystem(this);
        }
    }
}
